﻿using System;
using System.Html;
using Quicklight.Web.Html;
using jQueryApi;

namespace Quicklight.Web.Razor {

   /// <summary>
   /// Does the binding on behalf of a declarative binding of a ObservableProperty to the inner content of an element
   /// For example <div>@Binding(ViewModel.X)</div>
   /// </summary>
   /// <typeparam name="T">Type of the wrapped field of the ObservableProperty</typeparam>
   public class ObservablePropertyContentBinder<T> : ContentBinder {
      private ObservableProperty<T> property;

      /// <summary>
      /// Constructs a ObservablePropertyAttributeBinder for binding the ObservableProperty to the inner content of an element
      /// </summary>
      /// <param name="property">ObservableProperty to bind to the inner content of an element</param>
      public ObservablePropertyContentBinder(ObservableProperty<T> property) {
         this.property = property;
      }

      /// <summary>
      /// Bind method does the actual binding of the ObservableProperty to the inner content of the specified element
      /// </summary>
      /// <param name="element">Element with which the binding must occur</param>
      protected override void Bind(Element element) {
         element.Bind<T>(property);

         jQueryObject jQueryObject = jQuery.FromElement(element);
         jQueryObject.Text(property.Value.ToString());
      }
   }
}
